package cn.tedu.hotel.serve.service.impl;

import cn.tedu.hotel.serve.ex.ServiceException;
import cn.tedu.hotel.serve.mapper.HotelMapper;
import cn.tedu.hotel.serve.mapper.UserCommentMapper;
import cn.tedu.hotel.serve.mapper.UserMapper;
import cn.tedu.hotel.serve.pojo.dto.UserCommentDTO;
import cn.tedu.hotel.serve.pojo.entity.UserComment;
import cn.tedu.hotel.serve.pojo.vo.HotelStandardVO;
import cn.tedu.hotel.serve.pojo.vo.UserCommentVO;
import cn.tedu.hotel.serve.pojo.vo.UserStandardVO;
import cn.tedu.hotel.serve.service.IUserCommentService;
import cn.tedu.hotel.serve.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserCommentImpl implements IUserCommentService {

    @Autowired
    UserCommentMapper userCommentMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    HotelMapper hotelMapper;

    @Override
    public void addUserComment(UserCommentDTO userCommentDTO) {
        log.debug("开始处理【前台发表评论】的业务",userCommentDTO);
        UserStandardVO userStandardVO = userMapper.getStandardById(userCommentDTO.getUserId());
        if (userStandardVO==null){
            String message = "用户不存在，请先登录！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_BAD_REQUEST, message);
        }
        UserComment userComment=new UserComment();
        BeanUtils.copyProperties(userCommentDTO,userComment);
        int rows = userCommentMapper.addUserComment(userComment);
        if (rows!=1){
            String message = "发表评论失败,服务器繁忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public List<UserCommentVO> getHotelCommentListByHotelId(Long hotelId) {
        log.debug("开始处理【前台根据酒店ID：{}查询评论】的业务",hotelId);
        HotelStandardVO hotelStandardVO = hotelMapper.getHotelById(hotelId);
        if (hotelStandardVO==null){
            String message = "酒店Id不存在,请重新尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        List<UserCommentVO> list = userCommentMapper.getHotelCommentListByHotelId(hotelId);
        return list;
    }

    @Override
    public List<UserCommentVO> getAdminUserCommentList() {
        log.debug("开始处理【后台查询评论列表】的业务");
        List<UserCommentVO> list = userCommentMapper.getAdminUserCommentList();
        log.debug("开始处理【后台查询评论列表】的业务,数据：{}",list);
        return list;
    }

    @Override
    public void deleteUserCommentById(Long id) {
        log.debug("开始处理【后台根据评论ID：{}删除评论】的业务",id);
        UserCommentVO userCommentVO = userCommentMapper.getAdminUserComment(id);
        if (userCommentVO==null){
            String message = "评论不存在,请重新尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = userCommentMapper.deleteUserCommentById(id);
        if (rows!=1){
            String message = "服务器繁忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }
}
